package com.belf.web.controller.console;

import com.alibaba.fastjson2.JSONObject;
import com.belf.common.annotation.Log;
import com.belf.common.core.controller.BaseController;
import com.belf.common.core.domain.AjaxResult;
import com.belf.common.core.domain.entity.SysUser;
import com.belf.common.core.page.TableDataInfo;
import com.belf.common.enums.BusinessType;
import com.belf.common.utils.SecurityUtils;
import com.belf.common.utils.poi.ExcelUtil;
import com.belf.console.domain.AppraiseTasks;
import com.belf.console.domain.DataTaskLog;
import com.belf.console.service.IDataTaskLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 任务日志Controller
 * 
 * @author belf
 * @date 2024-06-20
 */
@RestController
@RequestMapping("/console/taskLog")
public class DataTaskLogController extends BaseController
{
    @Autowired
    private IDataTaskLogService dataTaskLogService;

    /**
     * 查询任务日志列表
     */
    @PreAuthorize("@ss.hasPermi('console:dataTaskLog:list')")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestBody DataTaskLog dataTaskLog)
    {
        // 管理员拥有所有权限
        if (!SysUser.isAdmin(SecurityUtils.getUserId())){
            dataTaskLog.setCreateBy(getUsername());
        }
        List<DataTaskLog> list = dataTaskLogService.selectDataTaskLogList(dataTaskLog);
        long count = dataTaskLogService.selectDataCount(dataTaskLog);
        return getDataTablePost(list,count);
    }

    /**
     * 导出任务日志列表
     */
    @PreAuthorize("@ss.hasPermi('console:dataTaskLog:export')")
    @Log(title = "任务日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DataTaskLog dataTaskLog)
    {
        List<DataTaskLog> list = dataTaskLogService.selectDataTaskLogList(dataTaskLog);
        ExcelUtil<DataTaskLog> util = new ExcelUtil<DataTaskLog>(DataTaskLog.class);
        util.exportExcel(response, list, "任务日志数据");
    }

    /**
     * 获取任务日志详细信息
     */
    @PreAuthorize("@ss.hasPermi('console:dataTaskLog:query')")
    @PostMapping("/getInfo")
    @ResponseBody
    public AjaxResult getInfo(@RequestBody DataTaskLog dataTaskLog)
    {
        return success(dataTaskLogService.selectDataTaskLogById(dataTaskLog.getId()));
    }


    @PostMapping("/getNew")
    @ResponseBody
    public AjaxResult getNew(@RequestBody DataTaskLog dataTaskLog)
    {
        return success(dataTaskLogService.selectDataTaskLogNew(dataTaskLog));
    }

    @PostMapping("/getNewLog")
    @ResponseBody
    public AjaxResult getNewLog(@RequestBody DataTaskLog dataTaskLog)
    {
        return dataTaskLogService.selectDataTaskLogNewInfo(dataTaskLog);
    }

    @PostMapping("/getJobResult")
    @ResponseBody
    public AjaxResult getJobResult(@RequestBody DataTaskLog dataTaskLog)
    {
        return dataTaskLogService.selectDataTaskLogResult(dataTaskLog);
    }

    /**
     * 新增任务日志
     */
    @PreAuthorize("@ss.hasPermi('console:dataTaskLog:add')")
    @Log(title = "任务日志", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(@RequestBody DataTaskLog dataTaskLog)
    {
        dataTaskLog.setCreateBy(getUsername());
        return toAjax((int) dataTaskLogService.insertDataTaskLog(dataTaskLog));
    }

    /**
     * 开放接口接收日志
     */
    @PostMapping("/push")
    @ResponseBody
    public AjaxResult push(@RequestBody JSONObject body)
    {
        return dataTaskLogService.pushDataTaskLog(body);
    }

    /**
     * 开放接口接收日志
     */
    @PostMapping("/pushResult")
    @ResponseBody
    public AjaxResult pushResult(@RequestBody JSONObject body)
    {
        return dataTaskLogService.pushDataTaskLogResult(body);
    }

    /**
     * 修改任务日志
     */
    @PreAuthorize("@ss.hasPermi('console:dataTaskLog:edit')")
    @Log(title = "任务日志", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult edit(@RequestBody DataTaskLog dataTaskLog)
    {
        dataTaskLog.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        return toAjax(dataTaskLogService.updateDataTaskLog(dataTaskLog));
    }

    /**
     * 删除任务日志
     */
    @PreAuthorize("@ss.hasPermi('console:dataTaskLog:remove')")
    @Log(title = "任务日志", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public AjaxResult remove(@RequestBody DataTaskLog dataTaskLog)
    {
        return toAjax(dataTaskLogService.deleteDataTaskLog(dataTaskLog));
    }

    @PreAuthorize("@ss.hasPermi('console:tasks:query')")
    @PostMapping("/getAppraiseTaskReportList")
    @ResponseBody
    public AjaxResult getAppraiseTaskReportList(@RequestBody AppraiseTasks appraiseTasks)
    {
        return success(dataTaskLogService.getAppraiseTaskReportList(appraiseTasks));
    }

}
